##############################################################
#Figure C9: First Stage in CCES Close Proximity
##############################################################

data <- read.dta13("./input/cces-zipstate.dta")

library(dplyr)
df <- data %>% filter(!is.na(stateid)) %>%
  group_by(stateid) %>%
  summarise_at(vars(risk,mindist), funs(mean(., na.rm=TRUE)))

df$statename[df$stateid==13] <- 	"DC"
df$statename[df$stateid==15] <- 	"WY"
df$statename[df$stateid==16] <- 	"VT"
df$statename[df$stateid==17] <- 	"HI"
df$statename[df$stateid==18] <- 	"AK"
df$statename[df$stateid==20] <- 	"DE"
df$statename[df$stateid==23] <- 	"MS"
df$statename[df$stateid==24] <- 	"NE"
df$statename[df$stateid==25] <- 	"MT"
df$statename[df$stateid==28] <- 	"RI"
df$statename[df$stateid==33] <- 	"AR"
df$statename[df$stateid==37] <- 	"WV"
df$statename[df$stateid==42] <- 	"AL"
df$statename[df$stateid==45] <- 	"NH"
df$statename[df$stateid==47] <- 	"ID"
df$statename[df$stateid==48] <- 	"ME"
df$statename[df$stateid==54] <- 	"IN"
df$statename[df$stateid==60] <- 	"NC"
df$statename[df$stateid==63] <- 	"UT"
df$statename[df$stateid==67] <- 	"NM"
df$statename[df$stateid==69] <- 	"GA"
df$statename[df$stateid==73] <- 	"KY"
df$statename[df$stateid==74] <- 	"SC"
df$statename[df$stateid==75] <- 	"OK"
df$statename[df$stateid==76] <- 	"NV"
df$statename[df$stateid==78] <- 	"CT"
df$statename[df$stateid==86] <- 	"NJ"
df$statename[df$stateid==99] <- 	"IA"
df$statename[df$stateid==106] <- 	"TN"
df$statename[df$stateid==107] <- 	"MO"
df$statename[df$stateid==123] <- 	"VA"
df$statename[df$stateid==124] <- 	"MI"
df$statename[df$stateid==132] <- 	"MD"
df$statename[df$stateid==136] <- 	"CO"
df$statename[df$stateid==143] <- 	"OR"
df$statename[df$stateid==171] <- 	"MA"
df$statename[df$stateid==198] <- 	"WI"
df$statename[df$stateid==211] <- 	"AZ"
df$statename[df$stateid==228] <- 	"WA"
df$statename[df$stateid==245] <- 	"NY"
df$statename[df$stateid==257] <- 	"OH"
df$statename[df$stateid==297] <- 	"IL"
df$statename[df$stateid==321] <- 	"FL"
df$statename[df$stateid==336] <- 	"PA"
df$statename[df$stateid==488] <- 	"TX"
df$statename[df$stateid==804] <- 	"CA"

labs <- filter(df, mindist < 1000) %>% 
  mutate(statename="",
         statename=ifelse(stateid==13, "DC", statename), 
         statename=ifelse(stateid==321, "FL", statename), 
         statename=ifelse(stateid==488, "TX", statename), 
         statename=ifelse(stateid==74, "SC", statename), 
         statename=ifelse(stateid==123, "VA", statename), 
         statename=ifelse(stateid==48, "ME", statename), 
         statename=ifelse(stateid==60, "NC", statename), 
         statename=ifelse(stateid==336, "PA", statename), 
         statename=ifelse(stateid==23, "MS", statename), 
         statename=ifelse(stateid==28, "RI", statename),
         statename=ifelse(stateid==16, "VT", statename),
         statename=ifelse(stateid==86, "NJ", statename),
         statename=ifelse(stateid==45, "NH", statename),
         statename=ifelse(stateid==106, "TN", statename)
         
         
         
         
  )

ggplot(data[data$mindist<1000,], aes(mindist, risk)) +
  stat_smooth(method="lm",fill="grey", size=2 , alpha=0.2 , colour="black") + 
  stat_smooth(method="loess",fill="grey", size=1 , alpha=0.2, col="grey60", linetype="dashed") + 
  geom_point(data=df[df$mindist<1000,], mapping=aes(x=mindist,y=risk), size=5, colour="goldenrod1", alpha=0.6, shape=19)  + 
  geom_text_repel(data=labs, aes(label=statename),size = 6) + ylab("Risk Aversion") + xlab("Distance (km)")  + 
  theme(text = element_text(size=14)) 
ggsave("./figures/figc9.pdf")
